//
//  BMWShareManager.swift
//  BMWShare
//
//  Created by Li Shengbing, (Shengbing.Li@partner.bmw.com) on 2019/6/10.
//  Copyright © 2019年 Li Shengbing, (Shengbing.Li@partner.bmw.com). All rights reserved.
//

import UIKit
import Foundation

public protocol ShareManagerProtocol {
    static var  shareComplete: ((_ result: String) -> ())? { get }
    static var  shareVersion: String { get }
    
    static func registerShareKey(types: [BMWShareManager.RegisterAppKey])
    static func handleOpen(url: URL)
    static func share(contents: [BMWShareManager.ContentKey: Any], platform: BMWShareManager.Platform, complete: ((_ result: String) -> ())?)
}

public class BMWShareManager: NSObject, ShareManagerProtocol {
    
    private static var weixinAppKey: String = ""
    private static var qqAppKey: String = ""
    private static var weiboAppKey: String = ""
    
    public static var shareComplete: ((String) -> ())? = nil
    public static var shareVersion: String {
        let bundle = Bundle(for: BMWShareManager.self)
        guard let dict = bundle.infoDictionary else { return "" }
        let version = dict["CFBundleShortVersionString"] as? String
        return version ?? ""
    }
    
    /// Register Appkey
    ///
    /// - Parameter type: RegisterAppKey
    public class func registerShareKey(types: [BMWShareManager.RegisterAppKey]){
        types.forEach {
            switch $0 {
                case .weixin(appKey: let _appKey):
                    BMWShareManager.weixinAppKey = _appKey
                    WXApi.registerApp(_appKey)

                case .qq(appKey: let _appKey):
                    BMWShareManager.qqAppKey = _appKey
                    _ = TencentOAuth(appId: _appKey, andDelegate: nil)

                case .weibo(appkey: let _appkey):
                    BMWShareManager.weiboAppKey = _appkey
                    _ = WeiboSDK.registerApp(_appkey)
            }
        }
    }

    /// Handle Open URL
    ///
    /// - Parameter url: url
    /// - Returns: HandleOpen Result
    public class func handleOpen(url: URL) {
        if let scheme = url.scheme,  scheme == BMWShareManager.weixinAppKey {
            _ = WXApi.handleOpen(url, delegate: ShareWX.shareInstance)
        }else if let scheme = url.scheme, scheme == "tencent" + BMWShareManager.qqAppKey {
            _ = QQApiInterface.handleOpen(url, delegate: ShareQQ.shareInstance)
        }else if let scheme = url.scheme, scheme == "wb" + BMWShareManager.weiboAppKey {
            _ = WeiboSDK.handleOpen(url, delegate: ShareWeibo.shareInstance)
        }
    }
    
    /// Share Function
    ///
    /// - Parameters:
    ///   - contents: content
    ///   - Platform: BMWShareManager.Platform
    public class func share(contents: [BMWShareManager.ContentKey: Any], platform: BMWShareManager.Platform, complete: ((_ result: String) -> ())? = nil) {
        self.shareComplete = complete
        switch platform {
        case .weixin(let scene):
            self.gotoWeixin(contents, scene)
        case .qq(let scene, let platform):
            self.gotoQQ(contents, scene, platform)
        case .weibo:
            self.gotoWeibo(contents)
        case .sms:
            self.gotoSMS(contents)
        case .copy:
            self.gotoCopy(contents)
        }
    }
}


extension BMWShareManager {
    
    /// Register App Key Enum
    ///
    /// - weixin: 微信key
    /// - qq: QQkey
    /// - weibo: 微博key
    
    public enum RegisterAppKey {
        case weixin(appKey: String)
        case qq(appKey: String)
        case weibo(appkey: String)
    }
    
    /// Platform Enum
    ///
    /// - weixin: 微信平台
    /// - qq: qq平台
    /// - weibo: 微博平台
    
    public enum Platform {
        case weixin(WXScene)
        case qq(ShareQQPlatform, ShareDestType)
        case weibo
        case sms
        case copy
    }
    
    /// Contents Enum
    ///
    /// - text: String <文字>
    /// - description: String <描述文字>
    /// - image: UIImage <图片>
    /// - url: String <网页链接>
    
    /// - previewImage: UIImage <预览图>
    /// - previewImageUrl: String <预览图链接>
    /// - audioUrl: String <音频链接>
    /// - audioDataUrl: String <音频数据链接>
    /// - vedioUrl: String <视频链接>
    /// - vedioLowBandUrl: String <视频低质量链接>
    /// - fileData: Data <文件数据>
    
    public enum ContentKey {
        case text
        case description
        case image
        case url
        
        case previewImage
        case previewImageUrl
        case audioUrl
        case audioDataUrl
        case videoUrl
        case videoLowBandUrl
        case fileData
    }
}
